package com.zw.weiyi.avtivity;

import java.util.ArrayList;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.zw.weiyi.enety.Person;
import com.zw.weiyi.phonesms.BaseActivity;
import com.zw.weiyi.phonesms.R;

/**
 * 添加联系人 
 * @author Striver_zw
 *  Date 2014-12-05
 */
public class AddPerson extends BaseActivity {
	private ImageButton p_im_addPerson, back;
	private EditText p_addnames, p_addnames2;
	private EditText p_addpnone, p_addohter;
	private String addnames, addnames2, addpnone, addohter;
	private Uri uri;
	private Person person;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info_addperson);
		initview();
		// 提示信息
		Toast.makeText(this, "该版本只能保存到手机中", 0).show();
	}

	// init
	public void initview() {
		p_im_addPerson = (ImageButton) findViewById(R.id.p_im_addPerson);
		back = (ImageButton) findViewById(R.id.back);
		p_addnames = (EditText) findViewById(R.id.p_addnames);
		p_addnames2 = (EditText) findViewById(R.id.p_addnames2);
		p_addpnone = (EditText) findViewById(R.id.p_addpnone);
		p_addohter = (EditText) findViewById(R.id.p_addohter);

		p_im_addPerson.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "未开发", 1000).show();
			}
		});
		
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	// 使用事务 写入手机中
	public void xie(View view) throws Exception {
		addnames = p_addnames.getText().toString();
		addnames2 = p_addnames2.getText().toString();
		addpnone = p_addpnone.getText().toString();
		addohter = p_addohter.getText().toString();

		if (TextUtils.isEmpty(addnames) || TextUtils.isEmpty(addpnone)) {
			Toast.makeText(this, "名字或电话不能为空不能", 1000).show();
			return;
		} else {
			inster(addnames, addnames2, addpnone, addohter);
			this.finish();
		}

	}

	public void inster(String addnames, String addnames2, String addpnone,
			String addohter) throws Exception {
		uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = this.getContentResolver();
		ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
		ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
				.withValue("account_name", null).build();
		operations.add(op1);
		if (!TextUtils.isEmpty(addnames)) {// 为空
			uri = Uri.parse("content://com.android.contacts/data");
			ContentProviderOperation op2 = ContentProviderOperation
					.newInsert(uri).withValueBackReference("raw_contact_id", 0)
					.withValue("mimetype", "vnd.android.cursor.item/name")
					.withValue("data1", addnames + addnames2).build();
			operations.add(op2);
		}

		if (!TextUtils.isEmpty(addpnone)) {
			ContentProviderOperation op3 = ContentProviderOperation
					.newInsert(uri).withValueBackReference("raw_contact_id", 0)
					.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
					.withValue("data1", addpnone).build();
			operations.add(op3);
		}
		// 邮箱
		if (!TextUtils.isEmpty(addohter)) {
			ContentProviderOperation op4 = ContentProviderOperation
					.newInsert(uri).withValueBackReference("raw_contact_id", 0)
					.withValue("mimetype", "vnd.android.cursor.item/email_v2")
					.withValue("data1", addohter).build();
			operations.add(op4);
		}
		resolver.applyBatch("com.android.contacts", operations);
		Toast.makeText(this, "添加成功", 1000).show();
	}
}
